package com.cloud.auth.controller;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.same.SaSameUtil;
import com.cloud.auth.domain.LoginReq;
import com.cloud.auth.service.LoginService;
import com.cloud.common.core.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 *  token 控制
 */
@RestController
@Slf4j
public class LoginController {

	@Autowired
	private LoginService loginService;

	/**
	 * 后台登陆
	 */
	@PostMapping("/login")
	public Result<?> login(@Validated @RequestBody LoginReq loginReq, HttpServletRequest req) {
		log.info("same_token:{}",req.getHeader(SaSameUtil.SAME_TOKEN));
		return loginService.login(loginReq);
	}

	/**
	 * 手机号登陆
	 * @desc
	 **/
	@PostMapping("/smsLogin")
	public Result<?>  smsLogin(String mobile) {
		return loginService.smsLogin(mobile);
	}

	/**
	 * 登出方法
	 */
	@GetMapping("/logout")
	public Result<?>  logout() {
		return loginService.logout();
	}

	@GetMapping("/open/test")
	public Result<?>  test() {
		String token= SaManager.getSaTokenDao().get("token:login:last-active:ed11fdfb-4515-4dac-a4ee-988183711f20");
		System.out.println(token);
		return Result.success();
	}

}
